package org.glickr

import org.glickr.enums.FlickrSizeSuffix

//import org.glickr.Utils

//<photo>
//  <comments>1</comments>
//  <notes>
//    <note id="313" author="12037949754@N01" authorname="Bees" x="10" y="10" w="50" h="50">foo</note>
//  </notes>
//  <tags>
//    <tag id="1234" author="12037949754@N01" raw="woo yay">wooyay</tag>
//    <tag id="1235" author="12037949754@N01" raw="hoopla">hoopla</tag>
//  </tags>
//  <urls>
//    <url type="photopage">http://www.flickr.com/photos/bees/2733/</url>
//  </urls>
//</photo>

class FlickrPhoto {
    Long id
    Long farmId
    Long serverId
    String secret
    String originalSecret
    String originalFormat   // todo Enum

    Integer license     // todo Enum
    Integer rotation
    String title
    String description
    Boolean isFavorite
    Boolean isPublic
    Boolean isFriend
    Boolean isFamily
    String contextUrl  // see flickr.photos.getContext
    List sizes = []
    List exif = []
    String camera
    Boolean canBlog
    Boolean canPrint
    Boolean canDownload
    Boolean canComment
    Boolean canAddMeta
    Integer permComment // todo Enum ?
    Integer permAddMeta // todo Enum ?
    Location location
    String dateTaken                    // todo convert to date ?
    Integer dateTakenGranularity        // todo Enum ?
    String datePosted                   // todo convert to date ?
    String dateLastUpdate               // todo convert to date ?
    
    FlickrPhoto prevPhoto       // <prevphoto id="2980" secret="973da1e709" title="boo!" url="/photos/bees/2980/" />
    FlickrPhoto nextPhoto       // <nextphoto id="2985" secret="059b664012" title="Amsterdam Amstel" url="/photos/bees/2985/" />

    private String sourceUrl
    private String shortUrl

    private final static shortUrlPrefix            = "http://flic.kr/p/"
    private final static sourceUrlTemplate         = "http://farm<%=farmId%>.staticflickr.com/<%=serverId%>/<%=photoId%>_<%=secret%>.jpg"
    private final static sourceSuffixUrlTemplate   = "http://farm<%=farmId%>.staticflickr.com/<%=serverId%>/<%=photoId%>_<%=secret%>_<%=sizeSuffix%>.jpg"
    private final static sourceOriginalUrlTemplate = "http://farm<%=farmId%>.staticflickr.com/<%=serverId%>/<%=photoId%>_<%=originalSecret%>_o.<%=originalFormat%>"

    // todo store orientation and aspectratio from url_t params

    String toString(String join='\n') {
        return this.properties.sort{it.key}.collect{it}.findAll{!['metaClass'].contains(it.key)}.join(join)
    }

    String getSourceUrl(FlickrSizeSuffix suffix=FlickrSizeSuffix.Medium) {
        if (!this.id || !this.farmId || !this.serverId) { return null }
        // todo if largest image is original, also check if originalSecret and originalFormat are available
        // todo fall back to the largest available image, check API and adjust suffix parameter
        // todo check if requested SizeSuffix in this.sizes

        // now render the resulting template
        def engine = new groovy.text.SimpleTemplateEngine()
        def binding = [
            photoId        : this.id,
            farmId         : this.farmId,
            serverId       : this.serverId,
            secret         : this.secret,
            originalSecret : this.originalSecret,
            originalFormat : this.originalFormat,
            sizeSuffix     : suffix.flickrSizeSuffix

        ]
        switch (suffix) {
            case FlickrSizeSuffix.Original:
                return engine.createTemplate(sourceOriginalUrlTemplate).make(binding).toString()
                break
            case FlickrSizeSuffix.Medium:
                return engine.createTemplate(sourceUrlTemplate).make(binding).toString()
                break
            default:
                return engine.createTemplate(sourceSuffixUrlTemplate).make(binding)
                break
        }
    }
    String getSourceWidth(FlickrSizeSuffix suffix=FlickrSizeSuffix.Medium) {
        def size = this.sizes?.find{it.suffix == suffix}
        return size ? size?.width : null
    }
    String getSourceHeight(FlickrSizeSuffix suffix=FlickrSizeSuffix.Medium) {
        def size = this.sizes?.find{it.suffix == suffix}
        return size ? size?.height : null
    }

    String getShortUrl() {
        return this.shortUrlPrefix + Utils.base58encode(this.id)
    }
}


class FlickrPhotoSize {
    FlickrSizeSuffix suffix
    Integer width
    Integer height
    String source
    String url
    String media

    String toString() {
        return suffix.toString() + ' (' + this.width + 'x' + this.height + ')'
    }
}

class FlickrPhotoExif {
    String tagSpace
    Integer tagSpaceId
    String tag
    String label
    String raw

    String toString(String join=', ') {
        return tagSpace + '.' + label + ': ' + raw
    }

}

/*
Note:
The Flickr API exposes identifiers for users, photos, photosets and other uniquely identifiable objects.
These IDs should always be treated as opaque strings, rather than integers of any specific type.
The format of the IDs can change over time, so relying on the current format may cause you problems in the future.
 */
